package com.shunda.trade.service.impl;


import com.shunda.trade.mapper.TradeOrderProfitMapper;
import com.shunda.trade.entity.TradeOrderProfit;
import com.shunda.trade.service.TradeOrderProfitService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 用户信息表(TradeOrderProfit)表服务实现类
 *
 * @author makejava
 * @since 2025-03-13 21:44:11
 */
@Service("tradeOrderProfitService")
public class TradeOrderProfitServiceImpl implements TradeOrderProfitService {
    @Resource
    private TradeOrderProfitMapper tradeOrderProfitDao;

    /**
     * 通过ID查询单条数据
     *
     * @param profitId 主键
     * @return 实例对象
     */
    @Override
    public TradeOrderProfit queryById(Long profitId) {
        return this.tradeOrderProfitDao.queryById(profitId);
    }

    /**
     * 分页查询
     *
     * @param tradeOrderProfit 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<TradeOrderProfit> queryByPage(TradeOrderProfit tradeOrderProfit, PageRequest pageRequest) {
        long total = this.tradeOrderProfitDao.count(tradeOrderProfit);
        return new PageImpl<>(this.tradeOrderProfitDao.queryAllByLimit(tradeOrderProfit, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param tradeOrderProfit 实例对象
     * @return 实例对象
     */
    @Override
    public TradeOrderProfit insert(TradeOrderProfit tradeOrderProfit) {
        this.tradeOrderProfitDao.insert(tradeOrderProfit);
        return tradeOrderProfit;
    }

    /**
     * 修改数据
     *
     * @param tradeOrderProfit 实例对象
     * @return 实例对象
     */
    @Override
    public TradeOrderProfit update(TradeOrderProfit tradeOrderProfit) {
        this.tradeOrderProfitDao.update(tradeOrderProfit);
        return this.queryById(tradeOrderProfit.getProfitId());
    }

    /**
     * 通过主键删除数据
     *
     * @param profitId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long profitId) {
        return this.tradeOrderProfitDao.deleteById(profitId) > 0;
    }

    @Override
    public TradeOrderProfit selectOrderProfitByOrderIdAndDeptId(Long deptId, Long orderId) {
        int sum = 0;
        TradeOrderProfit orderProfit = null;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            orderProfit = tradeOrderProfitDao.selectOrderProfitByOrderIdAndDeptId(deptId, orderId);
            sum++;
        } while (orderProfit == null || sum == 5);

        return orderProfit;
    }
}
